package com.football.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.football.springboot.common.Result;
import com.football.springboot.common.Return;
import com.football.springboot.entity.TeamData;
import com.football.springboot.service.ITeamDataService;
import com.football.springboot.service.ITeamService;
import com.football.springboot.vo.TeamDataQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.annotation.Resource;

@Api(tags = "球队数据管理")
@RestController
@RequestMapping("/teamdata")
public class TeamDataController {
    @Resource
    private ITeamDataService teamDataService;

    @Resource
    private ITeamService teamService;

    //查询全部
    @ApiOperation("查询全部")
    @GetMapping("/findall")
    public Result findAll() {
        return Result.success(teamDataService.list());
    }


    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("/pageTeamData")
    public Return pageTeamData(@RequestParam Integer current,
                               @RequestParam Integer limit,
                               TeamDataQueryVo teamDataQueryVo) {

        //创建page对象,传递当前页数current,每页记录数limit
        Page<TeamData> page = new Page<>(current,limit);
        //构建条件 模糊查询
        QueryWrapper<TeamData> queryWrapper = new QueryWrapper<>();

        String leagueSeason = teamDataQueryVo.getLeagueSeason();
        Integer teamId=teamDataQueryVo.getTeamId();
        Integer leagueId=teamDataQueryVo.getLeagueId();
        String homeAway=teamDataQueryVo.getHomeAway();

        //赛季查询
        queryWrapper.eq(!StringUtils.isEmpty(leagueSeason),"league_season",leagueSeason);
        //联赛查询
        queryWrapper.eq(leagueId!=null,"league_id",leagueId);
        //球队查询
        queryWrapper.eq(teamId!=null,"team_id",teamId);
        //主客场查询
        queryWrapper.eq(!StringUtils.isEmpty(homeAway),"home_away",homeAway);


        queryWrapper.orderByDesc("team_id","league_id");
        Page<TeamData> pageTeamData = teamDataService.page(page,queryWrapper);
        List<TeamData> records = pageTeamData.getRecords();
        records.forEach(teamData -> {
            //将球队信息插入到列表中
            teamData.setTeam(teamDataService.getteaminfo(teamData.getTeamId()));
            //将联赛信息插入到列表中
            teamData.setLeague(teamDataService.getleagueinfo(teamData.getLeagueId()));
        });

        return Return.ok().put("pageTeamData",pageTeamData).put("total",pageTeamData.getTotal());
    }



}
